package com.wn.procurement.web;

import com.wn.common.ActivitiStartEntity;
import com.wn.common.PageBean;
import com.wn.common.ResponseEntity;
import com.wn.entity.ReturnCargo;
import com.wn.procurement.openfeign.ActivitiFeignClient;
import com.wn.procurement.service.ReturnCargoService;
import com.wn.procurement.vo.ReturnCargoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/returnCargo")
public class ReturnCargoController {

    @Autowired
    private ReturnCargoService returnCargoService;

    @Autowired
    private ActivitiFeignClient activitiFeignClient;

    @RequestMapping("/add")
    public ResponseEntity add(@RequestBody @Validated ReturnCargo returnCargo , HttpServletRequest request){
        //returnCargoService.insert(returnCargo);
        ActivitiStartEntity activitiStartEntity = new ActivitiStartEntity();
        activitiStartEntity.setProcessDefinitionKey("returngoodsId");
        activitiStartEntity.setAssignee("user1");
        String workNumber= request.getHeader("userWorkNumber");

        ResponseEntity responseEntity = activitiFeignClient.startInstance(activitiStartEntity);

        if (responseEntity.getCode().equals("400")){
            return responseEntity.fail;
        }

        returnCargo.setActivitiInstanceId((String)responseEntity.getData());

        return ResponseEntity.success;
    }

//    @RequestMapping("update")
//    public ResponseEntity update(@RequestBody @Validated ReturnCargo returnCargo){
//        returnCargoService.update(returnCargo);
//        return ResponseEntity.success;
//    }

    @RequestMapping("/query")
    public ResponseEntity query(@RequestBody @Validated ReturnCargoVo returnCargoVo){
        PageBean<ReturnCargo> pageBean = returnCargoService.getByCondition(returnCargoVo);
        return new ResponseEntity(pageBean);
    }
}
